package com.easy.controller;

import com.easy.bean.Reservation;
import com.easy.service.ReservationService;
import com.easy.util.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("reservation")
public class ReservationController {

    @Autowired
    ReservationService reservationService;

    @PostMapping
    public Result add(@RequestBody Reservation reservation) {
        Object obj = reservationService.add(reservation);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(@RequestBody Reservation reservation) {
        Object obj = reservationService.edit(reservation);
        return Result.success("编辑信息保持成功", obj);
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id) {
        Object obj = reservationService.del(id);
        return Result.success("删除成功", obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id) {
        Object obj = reservationService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(Reservation reservation) {
        Object obj = reservationService.all(reservation);
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page(Page page, Reservation reservation) {
        // 查询一共有多少条数据
        // 当前页要显示的数据
        // 将以上数据封装到page对象中
        // 返回page对象
        reservationService.page(page, reservation);
        // 返回page对象
        return Result.success(page);
    }
    @GetMapping("pageBycustomer")
    public Result pageBycustomer(HttpServletRequest request, Page page, Reservation reservation) {

        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        try {
            // 从token中获取管理员ID
            Object idObj = TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);
            if (idObj == null) {
                return Result.error(500, "token异常");
            }
            int id = Integer.parseInt(idObj.toString());

            reservationService.pageBycustomer(id, page, reservation);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, e.getMessage());
        }
    }
}
